package com.longxi.tianjintianbao.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.utils.MyUtils;

/**
 * Created by cxw on & 2018/11/8
 */
public class AddComplaintFollowDialog extends Dialog {

    public AddComplaintFollowDialog(@NonNull Context context) {
        super(context);
    }

    public AddComplaintFollowDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddComplaintFollowDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private OnClickListener listener;
        private String title;
        private InputMethodManager imm;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            if("".equals(title)){
                this.title = "请设置标题";
            }else{
                this.title = title;
            }
            return this;
        }

        public Builder setListener(OnClickListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_title;
        private EditText et_content;
        private TextView tv_commit;

        private AddComplaintFollowDialog dialog;

        public AddComplaintFollowDialog create() {
            dialog = new AddComplaintFollowDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_add_complaint_follow, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_title = view.findViewById(R.id.tv_title);
            et_content = view.findViewById(R.id.et_content);
            tv_commit = view.findViewById(R.id.tv_commit);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            tv_commit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String describe = et_content.getText().toString().trim();
                    if(TextUtils.isEmpty(describe)){
                        MyUtils.showToast("请输入追记描述");
                        return;
                    }
                    listener.onClick(describe);
                }
            });
            return dialog;
        }

        public void clear(){
            et_content.setText("");
            et_content.setFocusable(true);
            et_content.setFocusableInTouchMode(true);
            et_content.requestFocus();
        }

        public void dismiss(){
            if(dialog != null){
                dialog.dismiss();
            }
        }

        public void show(){
            dialog.show();
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(40, 0, 40, 0);
        getWindow().setAttributes(layoutParams);
    }

    public interface OnClickListener {
        void onClick(String describe);
    }

}
